#include "ScnsIli9481.c.h"

#if defined(SCNS_ILI9481_ENABLE)&&SCNS_ILI9481_ENABLE==1

#include "ScnsSmctp.h"
#include "dma.h"
#include "stm32f103xe.h"
#include "ScnsLed.h"

const ScnsBspIli9481Config scnsBspIli9481Config[SCNS_ILI9481_MAX]={
    {
        .dir=0,
        .horizon=320,
        .vertical=480,
        .horizonBias=0,
        .verticalBias=0,
        .bgr=0,
        .reverseColor=0,
    },
};

#define TFTLCD_CMD  (*((volatile uint16*)0x6C000000))
#define TFTLCD_DAT  (*((volatile uint16*)(0x6C000000|(1<<11))))

ScnsStatus scnsBspIli9481Init(ScnsIli9481Enum ilN)
{
    return SCNS_STATUS_OK;
}

ScnsStatus scnsBspIli9481SetCs(ScnsIli9481Enum ilN,uint8 cs)
{
    return SCNS_STATUS_OK;
}

ScnsStatus scnsBspIli9481WReg(ScnsIli9481Enum ilN,uint8 dat)
{
    TFTLCD_CMD=dat;
    return SCNS_STATUS_OK;
}

ScnsStatus scnsBspIli9481WData(ScnsIli9481Enum ilN,uint8 dat)
{
    TFTLCD_DAT=dat;
    return SCNS_STATUS_OK;
}

ScnsStatus scnsBspIli9481WData16S(ScnsIli9481Enum ilN,uint16 dat[],uint32 len)
{
    for(uint32 i=0;i<len;TFTLCD_DAT=dat[i],++i);
    return SCNS_STATUS_OK;
}

static void callback(DMA_HandleTypeDef*pCallback)
{
    scnsIli9481AsyncCallback(SCNS_ILI9481_1);
}

ScnsStatus scnsBspIli9481WData16SAsync(ScnsIli9481Enum ilN,uint16 dat[],uint32 len)
{
    HAL_DMA_RegisterCallback(&hdma_memtomem_dma1_channel1,HAL_DMA_XFER_CPLT_CB_ID,callback);
    HAL_DMA_Start_IT(&hdma_memtomem_dma1_channel1,(uint32)dat,(uint32)&TFTLCD_DAT,len);
    return SCNS_STATUS_OK;
}

ScnsStatus scnsBspIli9481RData(ScnsIli9481Enum ilN,uint8 dat[],uint32 len)
{
    for(uint32 i=0;i<len;i+=2)
    {
        uint16 tmp=TFTLCD_DAT;
        dat[i+1]=tmp&0XFF;
        dat[i]=(tmp>>8)&0XFF;
    }
    return SCNS_STATUS_OK;
}

#endif
